package com.example.warehousems.controller;

import com.example.warehousems.component.BaseResponse;
import com.example.warehousems.component.UserInfo;
import com.example.warehousems.dao.model.Cargo;
import com.example.warehousems.service.WarehouseService;
import com.example.warehousems.vo.CargoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/api/warehouse")
@Slf4j
public class WarehouseController {
    @Autowired
    private WarehouseService warehouseService;

    /*
     * put cargo into storage
     * only for admin
     * after created cargo, it will be put into storage by publishing an event
     * */
    @PostMapping("/cargo")
    BaseResponse<String> in(@RequestBody @Validated CargoVO cargoVO) {
        if (!UserInfo.get("role").equals("admin")){
            return BaseResponse.error("无权访问");
        }
        Cargo cargo = new Cargo();
        cargo.setData(
                cargoVO.getName(),
                cargoVO.getLength(),
                cargoVO.getWidth(),
                cargoVO.getHeight(),
                cargoVO.getWeight(),
                cargoVO.getOutBranch()
        );
        cargo.setInTime(LocalDateTime.now());
        var res = warehouseService.putCargoIntoStorage(cargo);
        if (res == WarehouseService.RESULT.SUCCESS) {
            return BaseResponse.success("Warehouse in");
        } else if (res == WarehouseService.RESULT.IN_BUFFER) {
            return BaseResponse.error("put in buffer");
        } else {
            return BaseResponse.error("failed");
        }
    }

    @GetMapping("/outList")
    BaseResponse outList() {
        var res = warehouseService.showOutList();
        return BaseResponse.success(res);
    }

    /*
     * move cargo out
     * */
    @DeleteMapping("/{id}")
    BaseResponse out(@PathVariable Integer id) {
        warehouseService.moveCargoOut(id);
        return BaseResponse.success("Warehouse out");
    }

    /*
     * FOR TEST
     * clear old data, init new storage data
     * */
    @PostMapping("init")
    BaseResponse init(@RequestBody String s) {
        // simple auth
        if (!s.equals("init")) {
            return BaseResponse.error("failed");
        }
        warehouseService.initStorage();
        return BaseResponse.success("Warehouse init");
    }

    @GetMapping("storage")
    BaseResponse listStorage(@RequestParam("shelf") String shelf) {
        log.info("list storage of shelf {}", shelf);
        var res = warehouseService.listStorage(shelf);
        if (res == null) {
            log.info("test");
            return BaseResponse.error("无权访问");
        }

        return BaseResponse.success(res);
    }

    @GetMapping("storage/{shelf}/{floor}/{num}")
    BaseResponse getStorage(@PathVariable String shelf, @PathVariable Integer floor, @PathVariable Integer num) {
        if(!UserInfo.get("role").equals("admin")){
            return BaseResponse.error("无权访问");
        }
        var res = warehouseService.getStorage(shelf, floor, num);
        return BaseResponse.success(res);
    }

    @GetMapping("storage/cargo/{cargoId}")
    BaseResponse getStorageByCargoId(@PathVariable Integer cargoId) {
        if(!UserInfo.get("role").equals("admin")){
            return BaseResponse.error("无权访问");
        }
        var res = warehouseService.getStorageByCargoId(cargoId);
        return BaseResponse.success(res);
    }
}
